﻿<?php
class sscls_ProductController extends sscls_Controller
{
    function __construct()
    {
        $this->view='product';
    }
	
	function view()
	{
		$model=$this->getModel();
		$products=$model->getProductAll2();
		$values['model']=$model;
		$values['product']=$products['value'];
		$values['pagination']=$products['pagination'];
		$this->layout='products';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function details()
	{
		$model=$this->getModel();
		$op=sscls_Request::getRequest('o');
		$product_id=sscls_Request::getRequest('productid');
		$vals['item']=$model->getItem($product_id);
			
		if($op=='thuoc-tinh')		
			$vals['attri']=$model->getAttri($vals['item']->catid);
			
		if($op=='gallery')		
			$vals['images']=$model->getImages($product_id);
			
		if($op=='so-sanh')
		{
			$vals['attri']=$model->getAttri($vals['item']->catid);
			$vals['products']=$model->getProductAll();
			$vals['productImage1']=$model->getImage($product_id);
		}
		if($op=='bang-gia')
		{
			$vals['item']=$model->getItem($product_id);
			$vals['banggia']=$model->getArticle($vals['item']->pricetable);
		}
		
		$product_id_2=sscls_Request::getRequest('product_id_orther');
		if((int)$product_id_2)
		{
			$vals['item2']=$model->getItem($product_id_2);
			$vals['attri2']=$model->getAttri($vals['item2']->catid);
			$vals['productImage2']=$model->getImage($product_id_2);
		}else
		{
			$vals['item2']=$vals['item'];
			$vals['productImage2']=@$vals['productImage1'];
		}
			
			
		$vals['model']=$model;
		$this->layout='details';
		$this->view='product';
		$this->display($vals);
		return;
	}
	
	
	function attri()
	{
		$model=$this->getModel();
		$values['attries']=$model->getAttries();
		$this->layout='attries';
		$this->view='product';
		$this->display($values);
		return;
	}
	
	function addattri()
	{	
		
		$model=$this->getModel();
		$vals['cat']=$model->getCategories();
		$id=sscls_Request::getRequest('id');
		$vals['item']=$model->getAttriItem($id);
		$this->layout='addattri';
		$this->view='product';
		$this->display($vals);
		
	}
	
	function proccessaddattri()
	{
		$post=sscls_Request::get('post');
		$user=ss_Main::getUser();
		if($post['name']=='')
		{
			$this->setRedirec('index.php?c=product&t=addattri&id='.$post['id'],'Vui lòng nhập tên thuộc tính.','error');
			return;
		}
		$post['created_by']=$user->id;
		$post['created_date']=strtotime('now');
		$post['id']=sscls_Request::getRequest('id');
		$model=$this->getModel();
		
		if($model->store1($post,'product_attri'))
		{
			$this->setRedirec('index.php?c=product&t=attri','Đã lưu thành công.');
			return;
		}
		$this->setRedirec('index.php?c=product&t=addattri&id='.$post['id'],'Đã xảy ra lỗi trong quá trình lưu. Vui lòng thử lại.','error');
		return;
	}
	
    function add()
	{			
		$model=$this->getModel();
		if(isset($_POST['catid']))
			$vals['attri']=$model->getAttri($_POST['catid']);
		
		$vals['cat']=$model->getCategories();
		$id=sscls_Request::getRequest('id');
		$vals['item']=$model->getItem($id);
		
		if(isset($_GET['id'])&&@$_GET['id']!=0&&!isset($_POST['catid']))
		{
			$vals['attri']=$model->getAttri($vals['item']->catid);
		}
		$vals['model']=$model;
		$this->layout='add';
		$this->view='product';
		$this->display($vals);
		
	}
	function removeattri()
	{
		$cid=$_POST['cid'];
		$model=$this->getModel();
		$model->removeattri($cid);
		$this->setRedirec('index.php?c=product&t=attri');
		return;
	}
	function proccessadd()
	{
		$post=sscls_Request::get('post');
		$user=ss_Main::getUser();
		$post['created_by']=$user->id;
		if($post['productname']=='')
		{
			$this->setRedirec('index.php?c=product&t=add&id='.(int)$post['id'],'Vui lòng nhập tên sản phẩm.','error');
			return;
		}
		
		if($post['mode']=='')
		{
			$this->setRedirec('index.php?c=product&t=add&id='.(int)$post['id'],'Vui lòng nhập model sản phẩm.','error');
			return;
		}
		
		$model=$this->getModel();
		if($model->store1($post,'products'))
		{
			if((int)$post['id']>0)
				$productid=$post['id'];
			else
				$productid=$model->getID();
				
			$model->removeAttriByProductId($productid);
			$data=array();
			foreach((array)$post['idattri'] as $t=>$idattri)
			{
				$data['idattri']=$idattri;
				$data['product_id']=$productid;
				$data['value']=$post['attrivalue'][$t];
				$model->store1($data,'product_attri_value');
			}
			$this->setRedirec('index.php?c=product','Cập nhật thành công');
			return;
		}
		$this->setRedirec('index.php?c=product&t=add&id='.$post['id'],'Đã có lỗi trong quá trình lưu. Vui lòng thử lại','error');
		return;
	}
    
}
 
?>
